<?php
/**
 * Created by PhpStorm.
 * User: cdd
 * Date: 14-10-13
 * Time: 下午3:45
 */
require ('core/shop.php');
//查询订单主表类
quanli_require('module/base/bill/billSearch.class.php');
//字典操作类 itemHandler
quanli_require('module/base/system/itemHandler.class.php');
//退款单操作类
quanli_require('module/base/bill/billRefund.class.php');



$print=new bil_refund_print();
$print->method($_REQUEST['refund_no'],$_REQUEST['id']);

class bil_refund_print{


    function  method($refund_no,$id){

    global $G_PGLOB;

    //查询退款单信息
    $billRefund = new billRefund($G_PGLOB->DBCA);
    $arg_array = array(
        "refund_no" =>$refund_no
    );
    $result_refund = $billRefund->searchBillRefund($arg_array);


//查询商品信息
    if(!empty($result_refund[0]['return_no'])){         //有退货单先查退货商品
        $arg_array = array("return_no" => $result_refund[0]['return_no']);
        $result_goods = $billRefund->searchBillReturnGoods($arg_array);
        $G_PGLOB->smarty->assign("goods_array",$result_goods);
    }elseif(!empty($result_refund[0]['bill_no'])){
        $arg_array = array("bill_no" => $result_refund[0]['bill_no']);
        //实例化订单类
        $billSearch = new billSearch($G_PGLOB->DBCA);
        $result_goods = $billSearch->searchBillGoods($arg_array);
    }


    //查询此笔订单的 商品售价，实收运费，订单配件金额,代付订单 是否异地付款
    $bill_master_sql='select bill_no,bill_totalMoney,deliver_feePaid,bill_accessories_money,paying_agent_str,bill_other_address_pay from bill_master  where bill_no=?';
    $bill_info=$G_PGLOB->DBCA->stmt_query($bill_master_sql,'s',$result_refund[0]['bill_no']);

    $remote_service=0;//异地服务费
    $freight=$bill_info[0]->deliver_feePaid;//运费
    $accessories=$bill_info[0]->bill_accessories_money;//配件费
//    //此单为是否异地付款
//    if($bill_info[0]->bill_other_address_pay==1){
//        $remote_service=$bill_info[0]->bill_totalMoney;
//    }

        //查询是否为异地付款单
        $bill_goods_sql='select product_code,goods_purchasePrice from bill_goods where bill_no=? and  product_code=?';
        $bill_gods_query=$G_PGLOB->DBCA->stmt_query($bill_goods_sql,'ss',$bill_info[0]->bill_no,'ptp05');
        //异地服务费
        if(!empty($bill_gods_query)){
            foreach($bill_gods_query as  $goods){
                $remote_service+=$goods->goods_purchasePrice;
            }
        }

    //如果存在代付订单
    if($bill_info[0]->paying_agent_str){
        $bill_order=$bill_info[0]->paying_agent_str;//代付订单字符串
        //获取代付订单bill_master信息
        $bill_other_sql="select bill_code,bill_no,deliver_feePaid,bill_accessories_money from  bill_master where bill_code in($bill_order)";
        $bill_other_info=$G_PGLOB->DBCA->stmt_query($bill_other_sql);

        //循环代付订单
        foreach($bill_other_info as  $other_info){
            //把运费/配件费累加起来
            $freight+=$other_info->deliver_feePaid;
            $accessories+=$other_info->bill_accessories_money;
            //循环获取代付订单的商品信息(商品号，商品名称，规格，销售价，实收价，积分价，积分，购买数量)
            $bill_goods_sql="SELECT goods_goodsID,goods_name,goods_spec,goods_storePrice,goods_purchasePrice,goods_pointsPrice,goods_points,goods_amount FROM  bill_goods WHERE bill_no=?";
            $bill_goods_array=$G_PGLOB->DBCA->stmt_query($bill_goods_sql,'s',$other_info->bill_no);
            foreach($bill_goods_array  as $bill_goods){
                $other_goods_array[]=array(
                    'bill_code'=>$other_info->bill_code,
                    'goods_goodsID'=>$bill_goods->goods_goodsID,
                    'goods_name'=>$bill_goods->goods_name,
                    'goods_spec'=>$bill_goods->goods_spec,
                    'goods_storePrice'=>$bill_goods->goods_storePrice,
                    'goods_purchasePrice'=>$bill_goods->goods_purchasePrice,
                    'goods_pointsPrice'=>$bill_goods->goods_pointsPrice,
                    'goods_points'=>$bill_goods->goods_points,
                    'goods_amount'=>$bill_goods->goods_amount
                );
            }
        }
    }
    //查询退款单操作日志
    $refundLogSql='select * from log_detail where logCategoryid IN (1,11) AND  logItem=? order by createTime desc';
    $refundLog=$G_PGLOB->DBCA->stmt_query($refundLogSql,'s',$refund_no);

    $bill_refund = array(); //退款单信息数组
    $bill_refund = array(
        'tuikuandan'       =>  $refund_no, //退款单号
        'addtime'           =>  $result_refund[0]['createTime'],  //创建时间
        'status'            =>  $result_refund[0]['refund_status']==1 ? '未审核':($result_refund[0]['refund_status']==2 ? '已审核':($result_refund[0]['refund_status']==3 ? '取消' :'过账')),//状态
        'dingdan'           =>  $result_refund[0]['bill_code'], //订单号
        'tuikuanlaiyuan'   =>  $result_refund[0]['refund_source']==1 ? '订单退货' :($result_refund[0]['refund_source']==2 ? '订单取消' : '其他'),//退款来源
        'tuihuodan'         =>  $result_refund[0]['return_no'],   //退货单号
        'shoukuanren'       =>  $result_refund[0]['payee'],//收款人
        'tuikuanzhanghao'  =>  $result_refund[0]['refund_account'],//退款账号
        'tuikuanjine'       =>  $result_refund[0]['refund_amount'],//退款金额
        'huanwaifei'        => $freight,//环外费
        'peijianfei'        =>$accessories, //配件费
        'yidifuwufei'       =>  $remote_service,//异地服务费
        'bohuiyuanyin'      =>  $result_refund[0]['reject_cause'],//驳回原因
        'beizhu'             =>  $result_refund[0]['remark'],//备注
        'kaihuhang'         => $result_refund[0]['bank_deposit'], //开户行
        'yinhangkaguishu'  => $result_refund[0]['to_city'], //银行卡归属城市
        'tuikuanyuanyin'    =>$result_refund[0]['reason_refund']//退款原因
    );


    $bill_refund_goods = array();//退款单商品信息
    if(!empty($result_goods)){
        foreach($result_goods as $goods_result){

            $sql_fittings='select * from bill_goods_fittings where bill_goods_no =?';
            $bill_goods_no=$G_PGLOB->DBCA->stmt_query($sql_fittings,'i',$goods_result['bill_goods_no']);
            $buy_momney=0;
            foreach($bill_goods_no as $bill_buy){
                $buy_momney+=$bill_buy->buyMoney*$bill_buy->buyNum;
            }
            $bill_refund_goods[] = array(
                'shangpinhao2'                =>  $goods_result['goods_goodsID'],      //商品号
                'shangpinminchen2'           =>  $goods_result['goods_name'],          //商品名称
                'guige2'                      =>  $goods_result['goods_spec'],        //规格
                'xiaoshoujia2'               =>  $goods_result['goods_storePrice'],   //销售价
                'shishoujia2'                =>  $goods_result['goods_purchasePrice'],//实际售价
                'jifenjia2'                  =>  $goods_result['goods_pointsPrice'],  //积分价
                'jifen2'                     =>  $goods_result['goods_points'],       //积分
                'count2'                     =>$goods_result['goods_amount'],          //数量
                'peijianjiner'              =>$buy_momney        //配件费

            );
        }
    }

    $bill_refund_pay_goods = array();//退款单代付商品信息
    if(!empty($other_goods_array)){
        foreach($other_goods_array as $other_goods){
            $sql_fittings='select * from bill_goods_fittings where bill_goods_no =?';
            $bill_goods_no=$G_PGLOB->DBCA->stmt_query($sql_fittings,'i',$goods_result['bill_goods_no']);
            $buy_momney=0;
            foreach($bill_goods_no as $bill_buy){
                $buy_momney+=$bill_buy->buyMoney*$bill_buy->buyNum;
            }
            $bill_refund_pay_goods[] = array(
                'dingdanbianhao'              => $other_goods['bill_code'],
                'shangpinhao2'                =>  $other_goods['goods_goodsID'],      //商品号
                'shangpinminchen2'           =>  $other_goods['goods_name'],          //商品名称
                'guige2'                      =>  $other_goods['goods_spec'],        //规格
                'xiaoshoujia2'               =>  $other_goods['goods_storePrice'],   //销售价
                'shishoujia2'                =>  $other_goods['goods_purchasePrice'],//实际售价
                'jifenjia2'                  =>  $other_goods['goods_pointsPrice'],  //积分价
                'jifen2'                     =>  $other_goods['goods_points'],       //积分
                'count2'                     =>  $other_goods['goods_amount'],          //数量
                'peijianjiner'              =>$buy_momney         //配件费
            );
        }
    }


    $bill_refund_log = array();//操作日志
    foreach($refundLog as $refundLogBill){
        $bill_refund_log[] = array(
            'dotime'                        => $refundLogBill->createTime,             //操作时间
            'doname'                        =>  $refundLogBill->creator,               //操作人
            'tuikuandanxinxi'             =>  $refundLogBill->logItem,               //退款单信息
            'caozuoleixing'               =>  $refundLogBill->extend_1,              //操作类型
            'tuikuancaozuojilu'           =>  $refundLogBill->extend_2,             //退款单操作记录
            'tuikuanzhuangtai'            =>  $refundLogBill->extend_5 == 1 ? '未审核' :($refundLogBill->extend_5 == 2 ? '已审核' :($refundLogBill->extend_5 == 3 ? '取消' : '过账')),//退款单状态
        );
    };

    //查询该订单已过帐的退款信息
    $sql ="SELECT * FROM bill_refund WHERE bill_code = ? AND refund_no != ? AND refund_status = 4";
    $result_refunded = $G_PGLOB->DBCA->stmt_query($sql, 'ss', $result_refund[0]['bill_code'],$_REQUEST['refund_no']);

    //保存已过帐退款信息
        $bill_refund_posting = array();
    if($result_refunded){
        foreach($result_refunded as $key => $refunded){

            $bill_refund_posting[] = array(
                'guanliandanhao'                =>  $refunded->refund_no,     //关联单号
                'guozhangjine'                   =>   $refunded->refund_amount         //过帐金额

            );
        }
    }
    if($id==0){
         $this->bill_refund($bill_refund);
    }
    elseif($id==1){
        $this->bill_refund_goods($bill_refund_goods);
    }
    elseif($id==2){
        $this->bill_refund_pay_goods($bill_refund_pay_goods);
    }
    elseif($id==3){
        $this->bill_refund_log($bill_refund_log);
    }elseif($id==4){
        $this->bill_refund_posting($bill_refund_posting);
    }

}
/*
 * 退款单信息start
 */
function bill_refund($bill_refund){
    $xml_string ="<report>";
    $xml_string .="<_grparam>";
    foreach($bill_refund as $key => $val) {
        $xml_string .= "<$key>$val</$key>";
    }
    $xml_string .="</_grparam>";
    $xml_string .="</report>";
    header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等
    echo $xml_string;
}
/*
 * 退款单信息end
 */

/*
 * 退款单商品信息start
 */
    function bill_refund_goods($bill_refund_goods){
        $xml_string_refund_goods ="<report>";
        //$xml_string_refund_goods .="<xml>";
        $xml_string_refund_goods .="<rows>";
        foreach($bill_refund_goods as &$goods_refund){
            $xml_string_refund_goods .="<row>";
            foreach($goods_refund as  $key => $val){
                $xml_string_refund_goods .= "<$key>$val</$key>";
            }
            $xml_string_refund_goods .="</row>";
        }
        $xml_string_refund_goods .="</rows>";
        //$xml_string_refund_goods .="</xml>";
        $xml_string_refund_goods .="</report>";
        header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等
        echo $xml_string_refund_goods;
    }
/*
 * 退款单商品信息end
 */

/*
 * 代付订单商品信息start
 */
    function bill_refund_pay_goods($bill_refund_pay_goods){
        $xml_string_pay_goods ="<report>";
//        $xml_string_pay_goods .="<xml>";
        $xml_string_pay_goods .="<rows>";
        foreach($bill_refund_pay_goods as &$bill_pay_goods){
            $xml_string_pay_goods .="<row>";
            foreach($bill_pay_goods as  $key => $val){
                $xml_string_pay_goods .= "<$key>$val</$key>";
            }
            $xml_string_pay_goods .="</row>";
        }
        $xml_string_pay_goods .="</rows>";
//        $xml_string_pay_goods .="</xml>";
        $xml_string_pay_goods .="</report>";
        header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等
        echo $xml_string_pay_goods;

    }
/*
 * 代付订单商品信息end
 */


/*
 * 退款单操作日志start
 */
     function bill_refund_log($bill_refund_log){
        $xml_string_refund_log ="<report>";
//        $xml_string_refund_log .="<xml>";
        $xml_string_refund_log .="<rows>";
        foreach($bill_refund_log as &$refund_log){
            $xml_string_refund_log .="<row>";
            foreach($refund_log as  $key => $val){
                if($key == 'tuikuancaozuojilu')
                {
                    $xml_string_refund_log .= "<$key><![CDATA[$val]]></$key>";
                }
                else
                {
                    $xml_string_refund_log .= "<$key>$val</$key>";
                }

            }
            $xml_string_refund_log .="</row>";
        }

        $xml_string_refund_log .="</rows>";
//        $xml_string_refund_log .="</xml>";
        $xml_string_refund_log .="</report>";
        header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等
        echo $xml_string_refund_log;

    }
/*
 * 退款单操作日志end
 */


 /*
 * 已过帐信息
 */
    function bill_refund_posting($bill_refund_posting){
        $xml_string_pay_goods ="<report>";
//        $xml_string_pay_goods .="<xml>";
        $xml_string_pay_goods .="<rows>";
        foreach($bill_refund_posting as &$bill_pay_goods){
            $xml_string_pay_goods .="<row>";
            foreach($bill_pay_goods as  $key => $val){
                $xml_string_pay_goods .= "<$key>$val</$key>";
            }
            $xml_string_pay_goods .="</row>";
        }
        $xml_string_pay_goods .="</rows>";
//        $xml_string_pay_goods .="</xml>";
        $xml_string_pay_goods .="</report>";
        header("Content-Type: text/html; charset=utf-8"); //这里应根据实际运行环境调整，如：utf-8等
        echo $xml_string_pay_goods;

    }
    /*
     * 已过帐信息end
     */
}





